<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Worker\Controller;
use Think\Model;
use Worker\Logic\WorkerLogic;

/**
 * 消息控制器
 * @author derek <693472824@qq.com>
 */
class MessageController extends BaseController {
    
    /**
     * 小哥消息列表
     */
    public function msgList(){
        
        $account = I('account');
        
        //分页信息
        $page = I('POST.page');
        $page = is_numeric($page) && $page > 0 ? $page : 1;
        $start = ($page - 1) * $this->limit;
        
        $workerID = WorkerLogic::getIDByAccount($account);
        $worker = M('Worker')->where(array('worker_id'=>$workerID))->find();
        $memberType = $worker['member_type'];
        $falseType = $memberType == '1' ? '2' : '1';
        
        $map['Message.site_id'] = array('in', '0,'.$worker['site_id']);
        $map['create_time'] = array('EGT', NOW_TIME-604800);            //允许小哥查看一周内的消息
        $map['_string'] = ' (Message.msg_type != 1 and Message.worker_id="'.$workerID.'") OR (Message.msg_type = 1 AND Message.worker_id !="'.$falseType.'") ';
        
        $view = D('MessageView');
        $count = $view->where($map)->count();
        $totalPage = intval(floor(($count + $this->limit - 1)/$this->limit));
        
        //若查询当前页数大于总页数，则表示当前查询的是最后一页
        $lastPage = $page >= $totalPage ? true : false;
        
        if($page <= $totalPage){
            
            $list = $view->where($map)->limit($start, $this->limit)
                        ->field('msg_id,msg_type,content,create_time,receipt,order_id,order_no,emergency,delay,appointment')
                        ->order('receipt DESC,create_time DESC')->select();          //未收到回执时优先
            //$tp = $view->getLastSql();
        }
        
        $rspCont['lastPage'] = $lastPage;
        $rspCont['msgList'] = is_array($list) ? $list : array();
        
        $this->rsp['rspCode'] = '200';
        $this->rsp['rspCont'] = $rspCont;
        
        exit(json_encode($this->rsp));
    }
    
    
}
